package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.bean.Contract;
import com.tianxin.workbao.workBao.fragment.BaseFragment;
import com.tianxin.workbao.workBao.fragment.ContractCoalFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class ContractCoalActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mViewPager;
    private List<BaseFragment> mFragments;
    private FragmentStareAdapter mAdapter;
    private FloatingActionButton topFab;
    private Contract mContract;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_main);
        super.initStatusBar(true);

        initView();
        initData();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(R.string.contract_coal);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        setSupportActionBar(mToolbar);

        mViewPager = findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setCurrentItem(0);
        topFab = findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        mContract =(Contract) getIntent().getExtras().getSerializable("contract");
        mFragments = new ArrayList<>();
        mFragments.add(ContractCoalFragment.newInstance(mContract));
        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, null);
    }

    @Override
    public void initEvent() {
        mToolbar.setNavigationOnClickListener(view -> ContractCoalActivity.this.finish());
        topFab.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        mViewPager.setAdapter(mAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.base_data_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_add:
                Bundle bundle = new Bundle();
                bundle.putSerializable("contract", mContract);
                Intent intent = new Intent(this, AddContractCoalActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.action_Share:
                Toast.makeText(this, R.string.developing, Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onRestart() {
        updateUi();
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                updateUi();
                break;
        }
    }

    private void updateUi() {
        mFragments.get(0).initData(false);
    }
}
